/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.koron.audit.domain.Audit;
import com.koron.audit.service.api.AuditService;
import com.koron.common.dfs.FastDFSClientWrapper;
import com.koron.common.util.PdfUtil;
import com.koron.common.util.UserUtil;
import com.koron.common.util.WorkFlowConstant;
import com.koron.scene.domain.GasAnalysis;
import com.koron.scene.domain.OperationTicket;
import com.koron.scene.domain.SafetyMeasure;
import com.koron.scene.enums.OperationTicketEnum;
import com.koron.workflow.service.api.WorkflowService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.swan.bean.MessageBean;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;

@Component
public class OperationTicketPdfUtil {

    @Autowired
    private AuditService auditService;

    @Autowired
    private WorkflowService flowService;

    @Autowired
    private FastDFSClientWrapper clientWrapper;

    /**
     * 将作业票信息写到输出流中
     *
     * @param os              输出流
     * @param operationTicket 作业票信息
     */
    public void writeToOs(OutputStream os, OperationTicket operationTicket) {
        if (Objects.isNull(os) || Objects.isNull(operationTicket)) {
            return;
        }
        try {
            // 创建Document
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, os);
            // 打开document
            document.open();

            PdfPTable headTable = new PdfPTable(1);
            PdfPTable pdfPTable = new PdfPTable(16);

            // 标题字体格式
            Font titleFont = PdfUtil.getFont(12, Font.NORMAL);
            // 编号字体格式
            Font codeFont = PdfUtil.getFont(7, Font.NORMAL);
            // 表头字体格式
            Font headFont = PdfUtil.getFont(8, Font.NORMAL);
            // 内容字体格式
            Font contentFont = PdfUtil.getFont(8, Font.NORMAL);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");

            float contentPaddingBottom = 5f;
            float signPaddingBottom = 5f;

            // 获取流程审批数据
            Map<String, Map> flowNodeMap = new HashMap<>(4);
            Audit audit = auditService.queryByBusinessIds(new Gson().toJson(Collections.singletonList(operationTicket.getId())), operationTicket.getUpdateTime());
            try {
                if (!Objects.isNull(audit)) {
                    MessageBean<?> historyMsg = flowService.history(audit.getSetCode(), audit.getTemplateKey(), audit.getId());
                    if (historyMsg.getCode() == BusinessCode.SUCCESS.getCode() && !Objects.isNull(historyMsg.getData())) {
                        List<Map> historyList = (List<Map>) historyMsg.getData();
                        historyList.forEach(flowNode -> {
                            flowNodeMap.put(flowNode.get(WorkFlowConstant.STR_NOW_NODE_ID).toString(), flowNode);
                        });
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            int operationTicketType = Integer.parseInt(operationTicket.getOperationTicketType());

            if (operationTicketType == OperationTicketEnum.FIRE_SAFETY.getValue()) {
                PdfPCell pdfPCell = PdfUtil.createCell(OperationTicketEnum.FIRE_SAFETY.getPdfTitle(), headFont, Element.ALIGN_CENTER, 1, 1);
                pdfPCell.setBorderWidth(0f);
                headTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("编号：" + operationTicket.getOperationCode(), codeFont, Element.ALIGN_RIGHT, 1, 1);
                pdfPCell.setBorderWidth(0f);
                headTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业申请单位", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getApplicationUnitName(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业申请时间", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                if (Objects.isNull(operationTicket.getOperationApplyTime())) {
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                } else {
                    pdfPCell = PdfUtil.createCell(sdf.format(operationTicket.getOperationApplyTime()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                }
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业内容", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationContent(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("动火地点及动火部位", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getHotWorkLocationAndPosition(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("动火级别", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getHotWorkGrade(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("动火方式", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getHotWorkMode(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("动火人及证书编号", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getHotWorkPersonAndCertificate(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("监护人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationSupervisorName()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业单位", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationUnitName(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业负责人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationChargeName()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);

                List<GasAnalysis> gasAnalysisList = operationTicket.getGasAnalysisList();
                int gasAnalysisListSize = Math.min(gasAnalysisList.size(), 3);

                pdfPCell = PdfUtil.createCell("气体取样分析时间", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                for (int i = 0; i < gasAnalysisListSize; i++) {
                    if (Objects.isNull(gasAnalysisList.get(i).getGasAnalysisTime())) {
                        pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                    } else {
                        pdfPCell = PdfUtil.createCell(sdf.format(gasAnalysisList.get(i).getGasAnalysisTime()), contentFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                    }
                    pdfPTable.addCell(pdfPCell);
                }
                for (int i = 0; i < 3 - gasAnalysisListSize; i++) {
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                }
                pdfPCell = PdfUtil.createCell("代表性气体", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                for (int i = 0; i < gasAnalysisListSize; i++) {
                    pdfPCell = PdfUtil.createCell(gasAnalysisList.get(i).getRepresentGas(), contentFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                }
                for (int i = 0; i < 3 - gasAnalysisListSize; i++) {
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                }
                pdfPCell = PdfUtil.createCell("分析结果/%", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                for (int i = 0; i < gasAnalysisListSize; i++) {
                    pdfPCell = PdfUtil.createCell(gasAnalysisList.get(i).getAnalysisResult(), contentFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                }
                for (int i = 0; i < 3 - gasAnalysisListSize; i++) {
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                }
                pdfPCell = PdfUtil.createCell("分析人", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                for (int i = 0; i < gasAnalysisListSize; i++) {
                    pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(gasAnalysisList.get(i).getAnalystName()), contentFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                }
                for (int i = 0; i < 3 - gasAnalysisListSize; i++) {
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                }

                pdfPCell = PdfUtil.createCell("风险辨识结果", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getRiskIdentifyResult(), contentFont, Element.ALIGN_CENTER, 1, 12, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("动火作业实施时间", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                String operationTime = "";
                if (!Objects.isNull(operationTicket.getOperationStartTime()) && !Objects.isNull(operationTicket.getOperationEndTime())) {
                    operationTime = "自 " + sdf.format(operationTicket.getOperationStartTime()) + " 至 " + sdf.format(operationTicket.getOperationEndTime()) + "止";
                }
                pdfPCell = PdfUtil.createCell(operationTime, contentFont, Element.ALIGN_CENTER, 1, 12, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);

                // 生成安全措施列表信息
                generateSafetyMeasure(operationTicket, pdfPTable, headFont, contentFont, contentPaddingBottom);

                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_FIRST,
                        "作业负责人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "approveOperationChargeSignature", "approveOperationChargeName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_SECOND,
                        "作业区域负责人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "workUnitSignature", "workUnitName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_THIRD,
                        "安全员意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "safetyManageDeptSignature", "safetyManageDeptName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_FOURTH,
                        "作业审批人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "workApproverSignature", "workApproverName");
                generateConfirmCompleteContent(operationTicket, pdfPTable, headFont, contentFont, contentPaddingBottom, signPaddingBottom);

            } else if (operationTicketType == OperationTicketEnum.HIGH_ALTITUDE_SAFETY.getValue()) {
                PdfPCell pdfPCell = PdfUtil.createCell(OperationTicketEnum.HIGH_ALTITUDE_SAFETY.getPdfTitle(), headFont, Element.ALIGN_CENTER, 1, 1);
                pdfPCell.setBorderWidth(0f);
                headTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("编号：" + operationTicket.getOperationCode(), codeFont, Element.ALIGN_RIGHT, 1, 1);
                pdfPCell.setBorderWidth(0f);
                headTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业申请单位", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getApplicationUnitName(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业申请时间", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                if (Objects.isNull(operationTicket.getOperationApplyTime())) {
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                } else {
                    pdfPCell = PdfUtil.createCell(sdf.format(operationTicket.getOperationApplyTime()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                }
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业地点", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationLocation(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业内容", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationContent(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业高度", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationHeight(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("高处作业级别", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationHeightGrade(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业单位", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationUnitName(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("监护人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationSupervisorName()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationExecutorName()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业负责人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationChargeName()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("风险辨识结果", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getRiskIdentifyResult(), contentFont, Element.ALIGN_CENTER, 1, 12, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("动火作业实施时间", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                String operationTime = "";
                if (!Objects.isNull(operationTicket.getOperationStartTime()) && !Objects.isNull(operationTicket.getOperationEndTime())) {
                    operationTime = "自 " + sdf.format(operationTicket.getOperationStartTime()) + " 至 " + sdf.format(operationTicket.getOperationEndTime()) + "止";
                }
                pdfPCell = PdfUtil.createCell(operationTime, contentFont, Element.ALIGN_CENTER, 1, 12, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);

                // 生成安全措施列表信息
                generateSafetyMeasure(operationTicket, pdfPTable, headFont, contentFont, contentPaddingBottom);

                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_FIRST,
                        "作业负责人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "approveOperationChargeSignature", "approveOperationChargeName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_SECOND,
                        "作业区域负责人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "workUnitSignature", "workUnitName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_THIRD,
                        "安全员意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "safetyManageDeptSignature", "safetyManageDeptName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_FOURTH,
                        "作业审批人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "workApproverSignature", "workApproverName");
                generateConfirmCompleteContent(operationTicket, pdfPTable, headFont, contentFont, contentPaddingBottom, signPaddingBottom);

            } else if (operationTicketType == OperationTicketEnum.TEMPORARY_ELECTRICITY_SAFETY.getValue()) {
                PdfPCell pdfPCell = PdfUtil.createCell(OperationTicketEnum.TEMPORARY_ELECTRICITY_SAFETY.getPdfTitle(), headFont, Element.ALIGN_CENTER, 1, 1);
                pdfPCell.setBorderWidth(0f);
                headTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("编号：" + operationTicket.getOperationCode(), codeFont, Element.ALIGN_RIGHT, 1, 1);
                pdfPCell.setBorderWidth(0f);
                headTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业申请单位", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getApplicationUnitName(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业申请时间", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                if (Objects.isNull(operationTicket.getOperationApplyTime())) {
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                } else {
                    pdfPCell = PdfUtil.createCell(sdf.format(operationTicket.getOperationApplyTime()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                }
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业地点", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationLocation(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业内容", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationContent(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("电源接入点及许可用电功率", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getElectricPointcutPower(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("工作电压", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationVoltage(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("用电设备名称及额定功率", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationVoltage(), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("监护人", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationSupervisorName()), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("用电人", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getElectricCustomerName()), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationExecutorName()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("电工证号", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationExecutorElectricianCertificate(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业负责人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationChargeName()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("电工证号", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationChargeElectricianCertificate(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("风险辨识结果", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getRiskIdentifyResult(), contentFont, Element.ALIGN_CENTER, 1, 12, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("动火作业实施时间", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                String operationTime = "";
                if (!Objects.isNull(operationTicket.getOperationStartTime()) && !Objects.isNull(operationTicket.getOperationEndTime())) {
                    operationTime = "自 " + sdf.format(operationTicket.getOperationStartTime()) + " 至 " + sdf.format(operationTicket.getOperationEndTime()) + "止";
                }
                pdfPCell = PdfUtil.createCell(operationTime, contentFont, Element.ALIGN_CENTER, 1, 12, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);

                // 生成安全措施列表信息
                generateSafetyMeasure(operationTicket, pdfPTable, headFont, contentFont, contentPaddingBottom);

                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_FIRST,
                        "作业负责人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "approveOperationChargeSignature", "approveOperationChargeName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_SECOND,
                        "用电单位意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "electricConsumeUnitSignature", "electricConsumeUnitName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_THIRD,
                        "配送电单位意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "electricDistributionUnitSignature", "electricDistributionUnitName");
                generateConfirmCompleteContent(operationTicket, pdfPTable, headFont, contentFont, contentPaddingBottom, signPaddingBottom);

            } else if (operationTicketType == OperationTicketEnum.HOIST_SAFETY.getValue()) {
                PdfPCell pdfPCell = PdfUtil.createCell(OperationTicketEnum.HOIST_SAFETY.getPdfTitle(), headFont, Element.ALIGN_CENTER, 1, 1);
                pdfPCell.setBorderWidth(0f);
                headTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("编号：" + operationTicket.getOperationCode(), codeFont, Element.ALIGN_RIGHT, 1, 1);
                pdfPCell.setBorderWidth(0f);
                headTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业申请单位", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getApplicationUnitName(), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业单位", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationUnitName(), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业申请时间", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                if (Objects.isNull(operationTicket.getOperationApplyTime())) {
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                } else {
                    pdfPCell = PdfUtil.createCell(sdf.format(operationTicket.getOperationApplyTime()), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                }
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("吊装地点", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getLiftingLocation(), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("吊具名称", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getLiftingToolName(), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("吊物内容", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getLiftingContent(), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("吊装作业人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationExecutorName()), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("司索人", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getSisuoName()), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("监护人", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationSupervisorName()), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("指挥人员", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getHoistingCommanderName()), contentFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("吊物质量（t）及作业级别", headFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getLiftingWeightOperationGrade(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("风险辨识结果", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getRiskIdentifyResult(), contentFont, Element.ALIGN_CENTER, 1, 12, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业实施时间", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                String operationTime = "";
                if (!Objects.isNull(operationTicket.getOperationStartTime()) && !Objects.isNull(operationTicket.getOperationEndTime())) {
                    operationTime = "自 " + sdf.format(operationTicket.getOperationStartTime()) + " 至 " + sdf.format(operationTicket.getOperationEndTime()) + "止";
                }
                pdfPCell = PdfUtil.createCell(operationTime, contentFont, Element.ALIGN_CENTER, 1, 12, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);

                // 生成安全措施列表信息
                generateSafetyMeasure(operationTicket, pdfPTable, headFont, contentFont, contentPaddingBottom);

                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_FIRST,
                        "作业负责人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "approveOperationChargeSignature", "approveOperationChargeName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_SECOND,
                        "作业区域负责人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "workUnitSignature", "workUnitName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_THIRD,
                        "安全员意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "safetyManageDeptSignature", "safetyManageDeptName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_FOURTH,
                        "作业审批人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "workApproverSignature", "workApproverName");
                generateConfirmCompleteContent(operationTicket, pdfPTable, headFont, contentFont, contentPaddingBottom, signPaddingBottom);

            } else if (operationTicketType == OperationTicketEnum.FINITE_SPACE_SAFETY.getValue()) {
                PdfPCell pdfPCell = PdfUtil.createCell(OperationTicketEnum.FINITE_SPACE_SAFETY.getPdfTitle(), headFont, Element.ALIGN_CENTER, 1, 1);
                pdfPCell.setBorderWidth(0f);
                headTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("编号：" + operationTicket.getOperationCode(), codeFont, Element.ALIGN_RIGHT, 1, 1);
                pdfPCell.setBorderWidth(0f);
                headTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业申请单位", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getApplicationUnitName(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业申请时间", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                if (Objects.isNull(operationTicket.getOperationApplyTime())) {
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                } else {
                    pdfPCell = PdfUtil.createCell(sdf.format(operationTicket.getOperationApplyTime()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                }
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("受限空间名称", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getLimitSpaceName(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("受限空间内原有介质名称", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getLimitSpaceNeurogen(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业内容", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationContent(), contentFont, Element.ALIGN_CENTER, 1, 13, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业单位", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getOperationUnitName(), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业负责人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationChargeName()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationExecutorName()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("监护人", headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(operationTicket.getOperationSupervisorName()), contentFont, Element.ALIGN_CENTER, 1, 5, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);

                // 气体取样分析
                List<GasAnalysis> gasAnalysisList = operationTicket.getGasAnalysisList();
                int gasAnalysisListSize = Math.min(gasAnalysisList.size(), 2);

                pdfPCell = PdfUtil.createCell("气体分析", headFont, Element.ALIGN_CENTER, 5, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("分析项目", headFont, Element.ALIGN_CENTER, 2, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("有毒有害气体名称", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("可燃气体名称", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("氧气含量（体积分数）", headFont, Element.ALIGN_CENTER, 2, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("取样分析时间", headFont, Element.ALIGN_CENTER, 3, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("分析部位", headFont, Element.ALIGN_CENTER, 3, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("分析人", headFont, Element.ALIGN_CENTER, 3, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("H₂S", PdfUtil.getFont(PdfUtil.FONT_SEGUISYM_PATH, 8, Font.NORMAL), Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("苯", headFont, Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("甲烷", headFont, Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("CO", headFont, Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("合格标准", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("1~5.2", contentFont, Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPTable.addCell(pdfPCell);
                pdfPTable.addCell(pdfPCell);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("19.5%~21%", contentFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("分析数据", headFont, Element.ALIGN_CENTER, 2, 2, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                for (int i = 0; i < gasAnalysisListSize; i++) {
                    GasAnalysis gasAnalysis = gasAnalysisList.get(i);
                    pdfPCell = PdfUtil.createCell(gasAnalysis.getHydrogenSulfide(), contentFont, Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                    pdfPCell = PdfUtil.createCell(gasAnalysis.getBenzene(), contentFont, Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                    pdfPCell = PdfUtil.createCell(gasAnalysis.getMethane(), contentFont, Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                    pdfPCell = PdfUtil.createCell(gasAnalysis.getCarbonMonoxide(), contentFont, Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                    pdfPCell = PdfUtil.createCell(gasAnalysis.getOxygenConcentration(), contentFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                    if (Objects.isNull(gasAnalysis.getGasAnalysisTime())) {
                        pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                    } else {
                        pdfPCell = PdfUtil.createCell(sdf.format(gasAnalysis.getGasAnalysisTime()), contentFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                    }
                    pdfPTable.addCell(pdfPCell);
                    pdfPCell = PdfUtil.createCell(gasAnalysis.getAnalysisPosition(), contentFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                    pdfPCell = PdfUtil.createCell(UserUtil.getNameAndNumber(gasAnalysis.getAnalystName()), contentFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
                    pdfPTable.addCell(pdfPCell);
                }
                for (int i = 0; i < 2 - gasAnalysisListSize; i++) {
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 1, 20f);
                    pdfPTable.addCell(pdfPCell);
                    pdfPTable.addCell(pdfPCell);
                    pdfPTable.addCell(pdfPCell);
                    pdfPTable.addCell(pdfPCell);
                    pdfPCell = PdfUtil.createCell("", contentFont, Element.ALIGN_CENTER, 1, 2, 20f);
                    pdfPTable.addCell(pdfPCell);
                    pdfPTable.addCell(pdfPCell);
                    pdfPTable.addCell(pdfPCell);
                    pdfPTable.addCell(pdfPCell);
                }

                pdfPCell = PdfUtil.createCell("风险辨识结果", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell(operationTicket.getRiskIdentifyResult(), contentFont, Element.ALIGN_CENTER, 1, 12, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                pdfPCell = PdfUtil.createCell("作业实施时间", headFont, Element.ALIGN_CENTER, 1, 4, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);
                String operationTime = "";
                if (!Objects.isNull(operationTicket.getOperationStartTime()) && !Objects.isNull(operationTicket.getOperationEndTime())) {
                    operationTime = "自 " + sdf.format(operationTicket.getOperationStartTime()) + " 至 " + sdf.format(operationTicket.getOperationEndTime()) + "止";
                }
                pdfPCell = PdfUtil.createCell(operationTime, contentFont, Element.ALIGN_CENTER, 1, 12, contentPaddingBottom);
                pdfPTable.addCell(pdfPCell);

                // 生成安全措施列表信息
                generateSafetyMeasure(operationTicket, pdfPTable, headFont, contentFont, contentPaddingBottom);

                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_FIRST,
                        "作业负责人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "approveOperationChargeSignature", "approveOperationChargeName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_SECOND,
                        "作业区域负责人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "workUnitSignature", "workUnitName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_THIRD,
                        "安全员意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "safetyManageDeptSignature", "safetyManageDeptName");
                generateApproveContent(operationTicket, flowNodeMap, WorkFlowConstant.PROCESS_TASK_FOURTH,
                        "作业审批人意见：", pdfPTable, headFont, contentFont, contentPaddingBottom,
                        signPaddingBottom, "workApproverSignature", "workApproverName");
                generateConfirmCompleteContent(operationTicket, pdfPTable, headFont, contentFont, contentPaddingBottom, signPaddingBottom);

            }
            document.add(headTable);
            document.add(pdfPTable);
            document.close();
            writer.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 生成安全措施列表信息
     *
     * @param operationTicket
     * @param pdfPTable
     * @param headFont
     * @param contentFont
     * @param contentPaddingBottom
     */
    private void generateSafetyMeasure(OperationTicket operationTicket, PdfPTable pdfPTable, Font headFont, Font contentFont, float contentPaddingBottom) throws IOException, BadElementException {
        List<SafetyMeasure> safetyMeasureList = operationTicket.getSafetyMeasureList();

        PdfPCell pdfPCell = PdfUtil.createCell("序号", headFont, Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
        pdfPTable.addCell(pdfPCell);
        pdfPCell = PdfUtil.createCell("安全措施", headFont, Element.ALIGN_CENTER, 1, 13, contentPaddingBottom);
        pdfPTable.addCell(pdfPCell);
        pdfPCell = PdfUtil.createCell("落实情况", headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
        pdfPTable.addCell(pdfPCell);
        int safetyMeasureIndex = 1;
        for (SafetyMeasure safetyMeasure : safetyMeasureList) {
            pdfPCell = PdfUtil.createCell(String.valueOf(safetyMeasureIndex++), contentFont, Element.ALIGN_CENTER, 1, 1, contentPaddingBottom);
            pdfPTable.addCell(pdfPCell);
            pdfPCell = PdfUtil.createCell(safetyMeasure.getSafetyMeasureItem(), contentFont, Element.ALIGN_LEFT, 1, 13, contentPaddingBottom);
            pdfPTable.addCell(pdfPCell);
            Integer confirmStatus = safetyMeasure.getConfirmStauts();
            String confirmSituation = confirmStatus != null && confirmStatus == 1 ? "是" : "不涉及";
            pdfPCell = PdfUtil.createCell(confirmSituation, contentFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
            pdfPTable.addCell(pdfPCell);
        }
        String supplementaryMeasures = Objects.isNull(operationTicket.getSupplementaryMeasures()) ? StringUtils.EMPTY : operationTicket.getSupplementaryMeasures();
        pdfPCell = PdfUtil.createCell(String.valueOf(safetyMeasureIndex), contentFont, Element.ALIGN_CENTER, 2, 1, contentPaddingBottom);
        pdfPTable.addCell(pdfPCell);
        float signPaddingBottom = 5f;
        Paragraph signTitle = PdfUtil.createParagraph("其他安全措施：" + supplementaryMeasures, contentFont, Paragraph.ALIGN_LEFT);
        pdfPCell = PdfUtil.createCell(signTitle, Element.ALIGN_LEFT, 1, 15, signPaddingBottom);
        pdfPCell.setBorderWidthBottom(0f);
        pdfPTable.addCell(pdfPCell);

        pdfPCell = PdfUtil.createCell("", headFont, Element.ALIGN_CENTER, 1, 9, contentPaddingBottom);
        pdfPCell.setBorderWidthTop(0f);
        pdfPCell.setBorderWidthRight(0f);
        pdfPTable.addCell(pdfPCell);
        pdfPCell = PdfUtil.createCell("编制人：", headFont, Element.ALIGN_RIGHT, 1, 3, contentPaddingBottom);
        pdfPCell.setBorderWidthLeft(0f);
        pdfPCell.setBorderWidthTop(0f);
        pdfPCell.setBorderWidthRight(0f);
        pdfPTable.addCell(pdfPCell);
        Boolean flag = true;
        if (StringUtils.isNotEmpty(operationTicket.getEditOperationSignature())) {
            List<String> list = JSONObject.parseArray(operationTicket.getEditOperationSignature(), String.class);
            String url = Optional.ofNullable(list).get().get(0);
            InputStream inputStream = clientWrapper.downloadFile(url);
            if (null != inputStream) {
                Image image = Image.getInstance(IOUtils.toByteArray(inputStream));
                image.scaleAbsolute(40f, 20f);
                pdfPCell = PdfUtil.createCell(image, false, Element.ALIGN_LEFT, 1, 3, contentPaddingBottom);
                pdfPCell.setFixedHeight(30f);
                flag = false;
            }
        }
        if (flag) {
            String editUser = operationTicket.getAddUserName();
            pdfPCell = PdfUtil.createCell(editUser, contentFont, Element.ALIGN_LEFT, 1, 3);
        }
        pdfPTable.addCell(pdfPCell);

    }

    /**
     * 获取审批意见
     *
     * @param flowNodeMap
     * @param processTaskName
     * @return
     */
    private String getApprovalOpinion(Map<String, Map> flowNodeMap, String processTaskName) {
        Map flowNode = flowNodeMap.get(processTaskName);
        String comment = "";
        if (!Objects.isNull(flowNode)) {
            comment = flowNode.get(WorkFlowConstant.STR_COMMENT).toString();
            if (StringUtils.isEmpty(comment)) {
                comment = WorkFlowConstant.STR_AGREE;
            }
        }
        return comment;
    }

    /**
     * 获取审批人
     *
     * @param flowNodeMap
     * @param processTaskName
     * @return
     */
    private String getApprover(Map<String, Map> flowNodeMap, String processTaskName) {
        Map flowNode = flowNodeMap.get(processTaskName);
        String approver = "";
        if (!Objects.isNull(flowNode)) {
            List tasks = (ArrayList) flowNode.get("tasks");
            if (CollectionUtils.isEmpty(tasks)) {
                approver = flowNode.get(WorkFlowConstant.STR_OPERATION_USER_NAME).toString();
            } else {
                approver = ((Map) tasks.get(0)).get("userName").toString();
            }
        }
        return approver;
    }

    /**
     * 获取审批时间
     *
     * @param flowNodeMap
     * @param processTaskName
     * @return
     * @throws ParseException
     */
    private String getApprovalTime(Map<String, Map> flowNodeMap, String processTaskName) throws ParseException {
        Map flowNode = flowNodeMap.get(processTaskName);
        String approvalTime = "         年      月      日";
        if (!Objects.isNull(flowNode)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            approvalTime = sdf.format(sdf2.parse(flowNode.get(WorkFlowConstant.STR_OPERATION_TIME).toString()));
        }
        return approvalTime;
    }

    /**
     * 生成审批内容
     *
     * @param operationTicket
     * @param flowNodeMap
     * @param processTask
     * @param approveTitle
     * @param pdfPTable
     * @param headFont
     * @param contentFont
     * @param contentPaddingBottom
     * @param signPaddingBottom
     * @param signAttachment
     * @param signName
     * @throws IOException
     * @throws BadElementException
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     * @throws ParseException
     */
    private void generateApproveContent(OperationTicket operationTicket, Map<String, Map> flowNodeMap, String processTask, String approveTitle,
                                        PdfPTable pdfPTable, Font headFont, Font contentFont, float contentPaddingBottom,
                                        float signPaddingBottom, String signAttachment, String signName) throws IOException, BadElementException, NoSuchFieldException, IllegalAccessException, ParseException {
        String comment = getApprovalOpinion(flowNodeMap, processTask);
        Paragraph signTitle = PdfUtil.createParagraph(approveTitle + comment, contentFont, Paragraph.ALIGN_LEFT);
        PdfPCell pdfPCell = PdfUtil.createCell(signTitle, Element.ALIGN_LEFT, 1, 16, signPaddingBottom);
        pdfPCell.setBorderWidthBottom(0f);
        pdfPTable.addCell(pdfPCell);

        pdfPCell = PdfUtil.createCell("", headFont, Element.ALIGN_CENTER, 1, 9, contentPaddingBottom);
        pdfPCell.setBorderWidthTop(0f);
        pdfPCell.setBorderWidthRight(0f);
        pdfPTable.addCell(pdfPCell);

        pdfPCell = PdfUtil.createCell("签字：", headFont, Element.ALIGN_RIGHT, 1, 2, contentPaddingBottom);
        pdfPCell.setBorderWidthLeft(0f);
        pdfPCell.setBorderWidthTop(0f);
        pdfPCell.setBorderWidthRight(0f);
        pdfPTable.addCell(pdfPCell);

        Field signAttachmentField = OperationTicket.class.getDeclaredField(signAttachment);
        signAttachmentField.setAccessible(true);

        Object approverSignatureObj = signAttachmentField.get(operationTicket);
        if (!Objects.isNull(approverSignatureObj)) {
            String approverSignature = JSON.parseArray(approverSignatureObj.toString(), String.class).get(0);
            InputStream inputStream = clientWrapper.downloadFile(approverSignature);
            //TODO inputStream == null
            if (null != inputStream) {
                Image image = Image.getInstance(IOUtils.toByteArray(inputStream));
                image.scaleAbsolute(40f, 20f);
                pdfPCell = PdfUtil.createCell(image, false, Element.ALIGN_LEFT, 1, 2, contentPaddingBottom);
                pdfPCell.setFixedHeight(30f);
            }
        } else {
            pdfPCell = PdfUtil.createCell(getApprover(flowNodeMap, processTask), headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
        }
        pdfPCell.setBorderWidthLeft(0f);
        pdfPCell.setBorderWidthTop(0f);
        pdfPCell.setBorderWidthRight(0f);
        pdfPTable.addCell(pdfPCell);
        pdfPCell = PdfUtil.createCell(getApprovalTime(flowNodeMap, processTask), headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
        pdfPCell.setBorderWidthLeft(0f);
        pdfPCell.setBorderWidthTop(0f);
        pdfPTable.addCell(pdfPCell);
    }

    /**
     * 生成完工验收信息
     *
     * @param operationTicket
     * @param pdfPTable
     * @param headFont
     * @param contentFont
     * @param contentPaddingBottom
     * @param signPaddingBottom
     * @throws IOException
     * @throws BadElementException
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    private void generateConfirmCompleteContent(OperationTicket operationTicket, PdfPTable pdfPTable, Font headFont
            , Font contentFont, float contentPaddingBottom, float signPaddingBottom) throws IOException
            , BadElementException, NoSuchFieldException, IllegalAccessException {
        String completionDescription = Objects.isNull(operationTicket.getCompletionDescription()) ? StringUtils.EMPTY : operationTicket.getCompletionDescription();
        Paragraph signTitle = PdfUtil.createParagraph("完工验收意见：" + completionDescription, contentFont, Paragraph.ALIGN_LEFT);
        PdfPCell pdfPCell = PdfUtil.createCell(signTitle, Element.ALIGN_LEFT, 1, 16, signPaddingBottom);
        pdfPCell.setBorderWidthBottom(0f);
        pdfPTable.addCell(pdfPCell);

        pdfPCell = PdfUtil.createCell("", headFont, Element.ALIGN_CENTER, 1, 9, contentPaddingBottom);
        pdfPCell.setBorderWidthTop(0f);
        pdfPCell.setBorderWidthRight(0f);
        pdfPTable.addCell(pdfPCell);

        pdfPCell = PdfUtil.createCell("签字：", headFont, Element.ALIGN_RIGHT, 1, 2, contentPaddingBottom);
        pdfPCell.setBorderWidthLeft(0f);
        pdfPCell.setBorderWidthTop(0f);
        pdfPCell.setBorderWidthRight(0f);
        pdfPTable.addCell(pdfPCell);

        Field signAttachmentField = OperationTicket.class.getDeclaredField("completionConfirmSignature");
        signAttachmentField.setAccessible(true);

        Object approverSignatureObj = signAttachmentField.get(operationTicket);
        if (!Objects.isNull(approverSignatureObj)) {
            String approverSignature = JSON.parseArray(approverSignatureObj.toString(), String.class).get(0);
            InputStream inputStream = clientWrapper.downloadFile(approverSignature);
            //TODO inputStream == null
            if (null != inputStream) {
                Image image = Image.getInstance(IOUtils.toByteArray(inputStream));
                image.scaleAbsolute(40f, 20f);
                pdfPCell = PdfUtil.createCell(image, false, Element.ALIGN_LEFT, 1, 2, contentPaddingBottom);
                pdfPCell.setFixedHeight(30f);
            }
        } else {
            Field signNameField = OperationTicket.class.getDeclaredField("completionConfirmName");
            signNameField.setAccessible(true);
            String approveOperationChargeName = Objects.isNull(signNameField.get(operationTicket))
                    ? StringUtils.EMPTY : UserUtil.getNameAndNumber(signNameField.get(operationTicket).toString());
            pdfPCell = PdfUtil.createCell(approveOperationChargeName, headFont, Element.ALIGN_CENTER, 1, 2, contentPaddingBottom);
        }
        pdfPCell.setBorderWidthLeft(0f);
        pdfPCell.setBorderWidthTop(0f);
        pdfPCell.setBorderWidthRight(0f);
        pdfPTable.addCell(pdfPCell);
        String completionTime = "         年      月      日";
        if (!Objects.isNull(operationTicket.getCompletionTime())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日");
            completionTime = sdf.format(operationTicket.getCompletionTime());
        }
        pdfPCell = PdfUtil.createCell(completionTime, headFont, Element.ALIGN_CENTER, 1, 3, contentPaddingBottom);
        pdfPCell.setBorderWidthLeft(0f);
        pdfPCell.setBorderWidthTop(0f);
        pdfPTable.addCell(pdfPCell);
    }

}
